/*
 *  AMPStoriesHierarchyAdapter.cpp
 *  AMPlugin
 *
 *  Created by Vu Tiet on 8/10/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */


#include "VCPlugInHeaders.h"
#include "IStringListData.h"
#include "ListTreeViewAdapter.h"
#include "AMPStoryNodeID.h"
#include "AMPID.h"
#include "AMGlobal.h"

#include <iostream>
using namespace std;

/**
 *  TreeViewAdapter.
 */
class AMPStoriesHierarchyAdapter : public ListTreeViewAdapter
	{
	public:
		AMPStoriesHierarchyAdapter(IPMUnknown* boss);
		
		virtual ~AMPStoriesHierarchyAdapter()
		{}
		
		virtual int32	GetNumListItems() const;
		NodeID_rv GetRootNode() const;
		virtual NodeID_rv	GetNthListItem(const int32& nth) const;
		
		//virtual void Reload();
	};

CREATE_PMINTERFACE(AMPStoriesHierarchyAdapter, kAMPStoriesHierarchyAdapterImpl)

AMPStoriesHierarchyAdapter::AMPStoriesHierarchyAdapter(IPMUnknown* boss):ListTreeViewAdapter(boss)
{

	
	K2Vector<PMString> lists;
	for (int i = 0; i < g_assigneeSt.size(); i++) {
		
		string listItem = g_assigneeSt[i].storyID;
		//cout << "listItem: " << listItem << endl;
		PMString itemPMS(listItem.c_str());
		itemPMS.Translate();
		lists.push_back(itemPMS);
	}
	
	
	/*
	PMString name1("so1|Story so 1");
	name1.Translate();
	lists.push_back(name1);

	PMString name2("so2|Story so 2");
	name2.Translate();
	lists.push_back(name2);

	PMString name3("so3|Story so 3");
	name3.Translate();
	lists.push_back(name3);
	*/
	
	InterfacePtr<IStringListData> iListData(this, IID_ISTRINGLISTDATA);
	iListData->SetStringList(lists);

}
int32 AMPStoriesHierarchyAdapter::GetNumListItems()const
{	
	InterfacePtr<IStringListData> iListData(this, IID_ISTRINGLISTDATA);
	
	return iListData->GetStringList().size();
}

NodeID_rv AMPStoriesHierarchyAdapter::GetRootNode() const
{
	////cout << "AMPStoriesHierarchyAdapter::GetRootNode()" << endl;
	PMString rootString("Root");
	rootString.Translate();
	return AMPStoryNodeID::Create(rootString, rootString);
}

NodeID_rv AMPStoriesHierarchyAdapter::GetNthListItem(const int32& nth) const
{
	InterfacePtr<IStringListData> iListData(this, IID_ISTRINGLISTDATA);
	
	PMString curPMS = iListData->GetStringList()[nth];
	string node_id = curPMS.GrabCString();
	string node_name = "";
	if (nth >= 0 && nth < g_assigneeSt.size()) {
		node_id = g_assigneeSt[nth].storyID;
		node_name = g_assigneeSt[nth].storyName;
	}
	
	////cout << "curPMS - nodeid - nodename: " << curPMS.GrabCString() << " - " << node_id << " - " << node_name << endl;
	
	if (node_id != curPMS.GrabCString()) {
		for (int i = 0; i < g_assigneeSt.size(); i++) {
			if (g_assigneeSt[i].storyID == curPMS.GrabCString()) {
				node_id = g_assigneeSt[i].storyID;
				node_name = g_assigneeSt[i].storyName;
				break;
			}
		}
	}
	////cout << "node_name: " << node_name << endl;
	////cout << "curPMS - node_id - node_name: " << curPMS.GrabCString() << " - " << node_id << " - " << node_name << endl;
	PMString nodeIDPMS(node_id.c_str());
	PMString nodeNamePMS(node_name.c_str());
	return AMPStoryNodeID::Create(nodeIDPMS, nodeNamePMS);
	//return AMPStoryNodeID::Create(node_id, node_name);
}
